# -*- coding:utf-8 -*-
import json
import sys
import os

import re
import redis
import web
from mymodule.config import *
from mymodule.serverrun import serverrun

reload(sys)
sys.setdefaultencoding('utf-8')
urls = (
    "/runspider","run",
    '/load','load',
    '/force_stop','force_stop',
    '/stop','stop',
    '/test','test'
)
abspath = os.path.dirname(__file__)

run_app = web.application(urls, globals())

# 启动爬虫
class run:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        i = web.input()
        if(hasattr(i,'num')):num = i.num
        else:num=1
        if(hasattr(i,'start')):start = i.start
        else:start='http://www.dangdang.com'
        if not i.has_key('url_domain'):
            #g如果没有传入url_domain
            zz=r'((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?'
            zz_url_start = re.search(zz,start)

            #判断是否是网址
            if zz_url_start:
                domain = start.split('.')
                url_do ={}
                if len(domain)==2:
                    start_url = zz_url_start.group(1)+'www.'+zz_url_start.group(3)
                    redis_domain = start_url.split('.')
                    domain = redis_domain[1]
                    return self.isnew_run(domain=domain, start=start_url, num=num,)
                else:
                    domain = domain[1]
                    return self.isnew_run(domain=domain, start=start, num=num)
            else:
                return '传入的不是网址！'
        else:
            return 258369
            #如果传入url_domain
            domain = i['url_domain']
            self.isnew_run(domain=domain,start=start,num=num,isnew=False)

    def isnew_run(self,domain,start,num,isnew=True):
        r=getrdc()
        with open(path+'/url.json', 'w') as f:
            url_do = {}
            url_do['urldomain'] = domain
            f.write(json.dumps(url_do))
        if isnew:
            ss=str('task')
            task = r.lrange(ss, start=0, end=-1)
            # 取出任务，进行遍历状态
            if task:
                for i in task:
                    i = eval(i)
                    if i['taskName'] == domain:
                        run3 = serverrun(num=num, start=start, domain=domain, isnew=False)
                        re_data = {}
                        re_data['path'] = path+'/zrbspider/web/' + domain
                        re_data['taskname'] = domain
                        re_data['status'] = run3.run()
                        return json.dumps(re_data)
                    else:
                        run4 = serverrun(num=num, start=start, domain=domain)
                        re_data = {}
                        re_data['path'] = path+'/zrbspider/web/' + domain
                        re_data['taskname'] = domain
                        re_data['status'] = run4.run()
                        # return 4567
                        return json.dumps(re_data)
            else:
                run1 = serverrun(num=num, start=start, domain=domain)
                re_data = {}
                re_data['path'] = path+'/zrbspider/web/' + domain
                re_data['taskname'] = domain
                re_data['status'] = run1.run()
                return json.dumps(re_data)
        else:
            run2 = serverrun(num=num, start=start, domain=domain, isnew=False)
            re_data = {}
            re_data['path'] = path+'/zrbspider/web/' + domain
            re_data['taskname'] = domain
            run2.run()
            return json.dumps(re_data)
#获取请求数
class load:
    def GET(self):
        # f = os.getcwd()
        f = open(path+'/zrbspider/jiegouhua/jd.txt','r')
        return f.read(), '<hr>', sys.path, '<hr>',abspath



class force_stop:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        r=getrdc()
        jincheng = r.llen('process')
        print jincheng
        if jincheng:
            for i in range(jincheng):
                n = r.rpop('process')
                os.system('kill -s 9' + n)
        return 'stop ok'
class stop:
    def GET(self):
        x = web.input()
        url_domain = x['url_domain']
        r=getrdc()
        taskdata = r.lrange('task', start=0, end=-1)
        r.delete('task')
        for i in taskdata:
            i = eval(i)
            if i['taskName'] == url_domain:
                a = i['pid']
                for t in a:
                    os.system('kill -s 9 ' + str(t))
                    # n=r.rpop('process')
                    # return t
                i['pid'] = []
                i['taskStatus'] = 0
                i['taskNum'] = 0
            data = json.dumps(i)
            r.lpush('task', data)
        r.delete(url_domain + ':dupefilter', url_domain + ':requests')
        return 666

class test:
    def GET(self):
        r=getrdc()
        r.set('myname','jskgs')
        print r.get('myname')
        # r.lrange("namema",start=0,end=-1)
        return "run"